package com.monitor.controller;


import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.monitor.commons.HttpTookit;
import com.monitor.commons.SkJsonResult;
import com.monitor.commons.StringUtil;
import com.monitor.commons.enums.WhetherEnum;
import com.monitor.config.yunke.YunKeConfig;
import com.monitor.config.yunke.YunKeUtil;
import com.monitor.dto.CustomerDTO;
import com.monitor.dto.CustomerPageDTO;
import com.monitor.modelExt.CustomerExt;
import com.monitor.modelExt.UserExt;
import com.monitor.pubInter.IBaseService;
import com.monitor.pubInter.exception.SkException;
import com.monitor.searchExt.CustomerSearchExt;
import com.monitor.searchExt.UserSearchExt;
import com.monitor.service.ICustomerService;
import com.monitor.service.IUserService;
import com.monitor.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RequestMapping("/customer/")
@RestController(value = "customerController")
@Slf4j
public class CustomerController extends ApiManageController<CustomerDTO, CustomerPageDTO, CustomerExt, CustomerSearchExt, CustomerDomainVO, CustomerDetailDomainVO, CustomerDetailVO, CustomerListVO, String> {

    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IUserService userService;

    @Override
    public IBaseService<CustomerExt, CustomerSearchExt, String> getBaseService() throws SkException {
        return customerService;
    }

    @Autowired
    private YunKeUtil yunKeUtil;
    @Autowired
    private YunKeConfig yunKeConfig;


    /**
     * 查询所有员工的客户信息 并同步
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("queryCustomers")
    public SkJsonResult queryCustomers() throws Exception {
        customerService.syncCustomerData();
        return SkJsonResult.ok();
    }

    /**
     * 查询员工客户数量
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("queryCustomersCountGroupByUserId")
    public String queryCustomersCountGroupByUserId() throws Exception {
        //定义请求路径
        String url = yunKeConfig.getUrl() + "open/customer/getCustomersCountGroupByUserId";
        //组装请求头
        long timestamp = new Date().getTime();
        Map<String, String> headsMap = yunKeUtil.getHeadMap(timestamp);
        //组装请求参数
        Map<String, Object> paramsMap = new HashMap<>();
        List<String> ids=new ArrayList<>();
        ids.add("dd528b40a69540adb9cff67404418899");
        paramsMap.put("userIds",ids);
        String res = HttpTookit.doPostHeader(url, paramsMap, headsMap);
        JSONObject resObject = JSONObject.parseObject(res);
        if (resObject.getBoolean("success")) {
            JSONObject dataObj = resObject.getJSONObject("data");


        }
        return res;
    }
}
